package chan.project.smartlawcommon.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class FileContentExtractorUtil {

    /**
     * 提取上传文件中的文本内容
     *
     * @param file MultipartFile
     * @return 提取后的文本内容
     * @throws IOException 不支持的文件类型或读取失败时抛出
     */
    public static String extractText(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            throw new IllegalArgumentException("上传的文件为空");
        }
        String fileName = file.getOriginalFilename();
        Objects.requireNonNull(fileName, "文件名不能为空");

        if (fileName.endsWith(".txt")) {
            return readTxtFile(file);
        } else if (fileName.endsWith(".docx")) {
            return readDocxFile(file);
        } else if (fileName.endsWith(".xlsx")) {
            return readXlsxFile(file);
        } else if (fileName.endsWith(".pdf")) {
            return readPdfFile(file);
        } else {
            throw new UnsupportedOperationException("不支持的文件类型: " + fileName);
        }
    }

    // 读取 TXT 文件
    private static String readTxtFile(MultipartFile file) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
        }
        return content.toString();
    }

    // 读取 DOCX 文件
    private static String readDocxFile(MultipartFile file) throws IOException {
        try (XWPFDocument document = new XWPFDocument(file.getInputStream())) {
            XWPFWordExtractor extractor = new XWPFWordExtractor(document);
            return extractor.getText();
        }
    }

    // 读取 XLSX 文件
    private static String readXlsxFile(MultipartFile file) throws IOException {
        StringBuilder content = new StringBuilder();
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            for (Sheet sheet : workbook) {
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        content.append(cell.toString()).append("\t");
                    }
                    content.append("\n");
                }
            }
        }
        return content.toString();
    }

    // 读取 PDF 文件
    private static String readPdfFile(MultipartFile file) throws IOException {
        try (PDDocument document = PDDocument.load(file.getInputStream())) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }
}